//
//  RegistrationInformationConroller.m
//  这家测试项目
//
//  Created by IOS_DEVELOPER on 2017/6/5.
//  Copyright © 2017年 陈志凌. All rights reserved.
//

#import "ZJRegistrationInformationConroller.h"
#import "ZJRegistrationInformationSecConroller.h"
#import "ZJReviewVC.h"

@interface ZJRegistrationInformationConroller ()<UIActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>{
    BOOL idImg;
}
@property (weak, nonatomic) IBOutlet UITextField *userName; //真实姓名
@property (weak, nonatomic) IBOutlet UITextField *idCard; //身份证号码

@property (weak, nonatomic) IBOutlet UIButton *changeBtn;
@property (weak, nonatomic) IBOutlet UILabel *bottomLabel;

@property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *sexBtn; //性别按钮
@property (strong, nonatomic) IBOutletCollection(UIView) NSArray *informationView; //信息背景
@property (weak, nonatomic) IBOutlet UIButton *nextBtn; //下一步
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UIButton *idCardTop;
@property (weak, nonatomic) IBOutlet UIButton *idCardDown;

@property (assign, nonatomic) int sexBool;
@property (assign, nonatomic) BOOL okBool;

/**<图片数组>*/
@property (nonatomic,strong) NSMutableArray *photoArr;
@end

@implementation ZJRegistrationInformationConroller

- (void)viewDidLoad {
    [super viewDidLoad];
    self.photoArr = [NSMutableArray new];
    _sexBool = 1;
    _okBool = YES;
    //初始化设置
    for (UIButton * btn in self.sexBtn) {
        [btn setImage:[UIImage imageWithContentsOfFile:[Utils imgPath:@"tqbm_selected_xb@3x.png"]] forState:UIControlStateSelected];
    }
    UIButton * btn = self.sexBtn[0];
    btn.selected = YES;
    [self.userName becomeFirstResponder];
    self.bottomLabel.textColor = [Utils colorWithHexString:allConstHexColor];
    [self.changeBtn setImage:[UIImage imageWithContentsOfFile:[Utils imgPath:@"tqbm_icon_xy@3x.png"]] forState:0];
    [Utils fuwenbenLabel:self.bottomLabel FontNumber:[UIFont systemFontOfSize:15] AndRange:NSMakeRange(0, 3) AndColor:[Utils colorWithHexString:@"666666"]];
    
    [self.nextBtn setBackgroundColor:[Utils colorWithHexString:allConstHexColor]];
}

#pragma mark - 故事版按钮监听
- (IBAction)storyBtn:(UIButton *)sender {
    
    switch (sender.tag) {
        case 1000:
        {
            sender.selected = YES;
            UIButton * btn =self.sexBtn[1];

            btn.selected = NO;
            _sexBool = 1;
        }
            break;
        case 1001:
        {
            sender.selected = YES;
            UIButton * btn =self.sexBtn[0];

            btn.selected = NO;
            _sexBool = 2;
        }
            break;
        case 1002:
        {
            idImg = YES;
            UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"照片选择"
                                                                     delegate:self
                                                            cancelButtonTitle:@"取消"
                                                       destructiveButtonTitle:nil
                                                            otherButtonTitles:@"相机",@"相册", nil];
            [actionSheet showInView:self.view];
        }
            break;
        case 1003:
        {
            sender.selected = !sender.selected;
            _okBool = !sender.selected;

        }
            break;
        case 1004:
        {

            if ([NSString isNullOrEmpty:self.userName.text]) {
                [HUD showErrorMsg:@"请输入姓名"];
                return;
            }
            
            if ([NSString isNullOrEmpty:self.idCard.text]) {
                [HUD showErrorMsg:@"请输入身份证号码"];
                return;
            }
            if (![Utils checkContent:self.idCard.text regex:REGEX_IDENTITY_CARD]) {
                [HUD showErrorMsg:@"身份证格式错误"];
                return;
            }

            if (_okBool == NO) {
                [HUD showErrorMsg:@"请勾选同意公开信息"];
                return;
            }
            if (self.photoArr .count < 2 || !self.photoArr) {
                [HUD showErrorMsg:@"身份证信息不完整"];
                return;
            }
            [HUD showLoading:@"加载数据中..."];
            NSDictionary *parameter = [LRNetwork requestParameter:@{
                                                                    @"real_name":self.userName.text,
                                                                    @"gender":@(_sexBool),
                                                                    @"id_card":self.idCard.text,
                                                                    }];
            NSString *url = [LRNetwork requestURL:@"cgi/authenticate.php"];
            
            
            [LRNetwork netRequestPOSTImagesWithRequestURL:url withParameter:parameter withFileData:self.photoArr withReturnValeuBlock:^(id returnValue) {
                
                ZJReviewVC *vc = [[UIStoryboard storyboardWithName:@"Home" bundle:nil] instantiateViewControllerWithIdentifier:@"ZJReviewVC"];
                vc.isBackRoot = self.isBackRoot;
                [self.navigationController pushViewController:vc animated:YES];
                
            } withErrorCodeBlock:^(NSInteger errorCode, NSString *errorMessage) {
                
            } withFailureBlock:^{
                
            } withUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
                
            }];
        }
            break;
            
        case 1005:
        {
            idImg = NO;
            if (self.photoArr.count <1) {
                [HUD showErrorMsg:@"请先添加正面照片"];
            }else{
            UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"照片选择"
                                                                     delegate:self
                                                            cancelButtonTitle:@"取消"
                                                       destructiveButtonTitle:nil
                                                            otherButtonTitles:@"相机",@"相册", nil];
            [actionSheet showInView:self.view];
            }
        }
            break;
            
        default:
            break;
    }
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    //先设定sourceType为相机，然后判断相机是否可用（ipod）没相机，不可用将sourceType设定为相片库
    UIImagePickerController *pickerImage = [[UIImagePickerController alloc] init];
    pickerImage.delegate = self;
    pickerImage.allowsEditing = YES;//设置可编辑
    switch (buttonIndex) {
        case 0:
        {
            NSLog(@"相机");
            pickerImage.sourceType = UIImagePickerControllerSourceTypeCamera;
            [self presentViewController:pickerImage animated:YES completion:nil];//进入照相界面
        }
            break;
        case 1:
        {
            NSLog(@"相册");
            pickerImage.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; //图片库
            [self presentViewController:pickerImage animated:YES completion:nil];//进入照相界面
        }
            break;
        case 2:
        {
            NSLog(@"取消");
        }
            break;
    }
    
}
//点击相册中的图片 货照相机照完后点击use  后触发的方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [picker dismissViewControllerAnimated:YES completion:^{}];
    
    UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
    if (idImg) {
        [self.idCardTop setImage:image forState:0];
        if (self.photoArr.count >=1) {
            [self.photoArr replaceObjectAtIndex:0 withObject:image];
            
        }else{
        [self.photoArr addObject:image];
        }
    }else{
        [self.idCardDown setImage:image forState:0];
        if (self.photoArr.count ==2) {
            [self.photoArr replaceObjectAtIndex:1 withObject:image];
            
        }else{
            [self.photoArr addObject:image];
        }
    }
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [picker dismissViewControllerAnimated:YES completion:^{}];
}

@end
